package com.gs.core.service.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.gs.core.domain.User;
import com.gs.core.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@PostConstruct
	public void initUsers(){
		User user = User.create("Kalyan","Dasika");
		save(user);
		user = User.create("Madhavi","Dasika");
		save(user);
		user = User.create("Rudrakshi","Dasika");
		save(user);
		user = User.create("Krishna","Dasika");
		save(user);
		user = User.create("Sahasrakshi","Dasika");
		save(user);		
	}
	
	private final Set<User> users = new HashSet<User>();
	private final Map<String,User> userMap = new HashMap<String,User>();
	@Override
	public void save(User user) {
		if(user.getUserId()==null){
			user.setUserId(user.getFirstName().substring(0,3)+user.getLastName().substring(0,3)+Integer.valueOf(users.size()).toString());
		}
		users.add(user);
		userMap.put(user.getUserId(), user);
	}
	@Override
	public User get(String userId) {
		return userMap.get(userId);
	}
	@Override
	public Collection<User> findAll() {
		return users;
	}
	
	

}
